// 2025/10/10: 首个版本
// 2025/10/10: 优化图标显示
// 2025/11/24: 优化暗色模式下的颜色显示

using System;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using Avalonia.Media;
using Avalonia.Controls;
using Avalonia.Threading;
using ASEva;
using ASEva.UIAvalonia;
using ASEva.Utility;

namespace CommonSource
{
    partial class CheckDeviceStatus : DialogPanel
    {
        public CheckDeviceStatus()
        {
            InitializeComponent();
            SetResizableMode(400, 200, 700, 420);
            language = new LanguageSwitch(Resources, AgencyLocal.GetAppLanguage());
            Title = language["title"];
            DataContext = model;

            if (FuncManager.Run("System.GetDesignerModeFlag") == null)
            {
                timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromMilliseconds(200);
                timer.Tick += timer_Tick;
                timer.Start();
            }
        }

        public String IconResourceName
        {
            set => Icon = ResourceLoader.Load(value)?.ToAvaloniaWindowIcon();
        }

        public override void OnClosing()
        {
            timer?.Stop();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            queryTask.Handle(async () =>
            {
                var chinese = AgencyLocal.GetAppLanguage() == Language.Chinese;

                var infos = await AgencyAsync.GetAllDeviceStatus();
                var types = infos.Keys.ToArray();

                model.DeviceInfoItems.Clear();
                for (int i = 0; i < types.Length; i++)
                {
                    var type = types[i];
                    var status = infos[type].Status;
                    var detail = infos[type].Description;
                    if (String.IsNullOrWhiteSpace(detail)) detail = chinese ? "(无)" : "(None)";

                    IBrush textColor = App.DarkTheme ? Brushes.LightGray : Brushes.DarkGray;
                    switch (status)
                    {
                        case GeneralDeviceStatus.Error:
                            textColor = Brushes.Red;
                            break;
                        case GeneralDeviceStatus.Warning:
                            textColor = Brushes.DarkOrange;
                            break;
                        case GeneralDeviceStatus.OK:
                            textColor = App.DarkTheme ? Brushes.LimeGreen : Brushes.Green;
                            break;
                    }

                    model.DeviceInfoItems.Add(new DeviceInfoItem
                    {
                        Type = type,
                        Info = detail,
                        Foreground = textColor,
                    });
                }
            });
        }

        private partial class DeviceInfoItem : ObservableObject
        {
            [ObservableProperty]
            private String type;

            [ObservableProperty]
            private String info;

            [ObservableProperty]
            private IBrush foreground = App.DarkTheme ? Brushes.White : Brushes.Black;
        }

        private partial class Model : ObservableObject
        {
            [ObservableProperty]
            private ObservableCollection<DeviceInfoItem> deviceInfoItems = new();
        }

        private Model model = new();
        private LanguageSwitch language;
        private DispatcherTimer timer;
        private TaskBeat queryTask = new();
    }
}